package com.tmpt.service.impl;

import com.googlecode.genericdao.search.Search;
import com.inspiresoftware.lib.dto.geda.assembler.DTOAssembler;
import com.tmpt.beans.*;
import com.tmpt.dao.intf.*;
import com.tmpt.dto.GoodsInfoDto;
import com.tmpt.dto.GoodsStackInfoDto;
import com.tmpt.dto.StockInfoDto;
import com.tmpt.service.GoodsInfoService;
import com.tmpt.dto.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lvgang on 2017/1/10.
 */
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class GoodsInfoServiceImpl implements GoodsInfoService {
    @Resource
    private ImageInfoDao imageInfoDao;
    @Resource
    private GoodsInfoDao goodsInfoDao;
    @Resource
    private GoodsCategoryLinkDao goodsCategoryLinkDao;
    @Resource
    private StockInfoDao stockInfoDao;
    @Resource
    private GoodsStockInfoDao goodsStockInfoDao;
    @Override
    public Result addGoodsInfo(GoodsInfoDto goodsInfoDto) {
        Result result = new Result();
        GoodsInfo goodsInfo = new GoodsInfo();
        //assemble dto
        DTOAssembler assembler = DTOAssembler.newAssembler(GoodsInfoDto.class);
        assembler.assembleEntity(goodsInfoDto, goodsInfo, null, null);
        goodsInfoDao.create(goodsInfo);
        for(GoodsCategoryLink link : goodsInfo.getGoodsCategoryLinks()){
            link.setGoods_info(goodsInfo);
            goodsCategoryLinkDao.create(link);
        }
        return result.success();
    }

    @Override
    public Result addGoodsImageLink(int imageId, int goodsInfoId) {
        Result result = new Result();
        GoodsInfo goodsInfo = goodsInfoDao.fetch(goodsInfoId);
        ImageInfo imageInfo = imageInfoDao.fetch(imageId);
        imageInfo.setGoods_info(goodsInfo);
        imageInfo.setModifydate(new Date());
        imageInfoDao.update(imageInfo);
        result.success();
        return result;
    }

    @Override
    public Result addGoodsStockLink(int stockId, int goodsInfoId, GoodsStackInfoDto dto) {
        Result result = new Result();
        GoodsInfo goodsInfo = goodsInfoDao.fetch(goodsInfoId);
        StockInfo stockInfo = stockInfoDao.fetch(stockId);
        GoodsStackInfo goodsStackInfo = new GoodsStackInfo();
        DTOAssembler ass = DTOAssembler.newAssembler(GoodsStackInfoDto.class);
        ass.assembleEntity(dto, goodsStackInfo, null, null);
        goodsStackInfo.setGoods_info(goodsInfo);
        goodsStackInfo.setStack(stockInfo);
        goodsStockInfoDao.create(goodsStackInfo);
        result.success();
        return result;
    }

    @Override
    public List<StockInfoDto> queryStockInfoByMchntId(int mchntId) {
        Search search = new Search();
        search.addFilterEqual("mchntId", mchntId);
        List<StockInfo> stockInfos = stockInfoDao.search(search);
        List<StockInfoDto> stockInfoDtos = new ArrayList<StockInfoDto>();
        DTOAssembler ass = DTOAssembler.newAssembler(StockInfoDto.class);
        ass.assembleDtos(stockInfoDtos, stockInfos, null, null);
        return stockInfoDtos;
    }
}
